package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author hsg
 * @since 2022-06-27
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
/*        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getApUserId,user.getApUserId());
        WmUser wmUser = this.getOne(queryWrapper);
        if(wmUser==null){
            wmUser.setCreatedTime(new Date());
            this.save(user);
        }
        return ResponseResult.okResult(user);*/
            // 判断用户是否存在,根据 ap_user_id 进行判断
            LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
            query.eq(WmUser::getApUserId, user.getApUserId());
            WmUser one = this.getOne(query);
            if(one==null){
                user.setCreatedTime(new Date());
                this.save(user);
            }
            return ResponseResult.okResult(user);

    }

    @Override
    public ResponseResult login(LoginDto loginDto) {
        if(loginDto==null || StringUtils.isBlank(loginDto.getName()) || StringUtils.isBlank(loginDto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //一般情况下用户名和密码分开做，因为用户名一般有唯一约束，就会自动创建索引，只通过用户名查询数据效率高
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getName,loginDto.getName());
        WmUser one = this.getOne(queryWrapper);
        if(one==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //密码是通过md5加密，但是因为md5的缺陷/特点（加密后的数据固定，不可逆），不太安全，数据库里边有个字段salt，来对原数据进行加盐之后再生成密文

        String password_md5 = DigestUtils.md5Hex(loginDto.getPassword() + one.getSalt());
        if (!StringUtils.equals(password_md5, one.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //返回的是一个map。其中一个是user对应对象，另一个是token对应字符串
        Map resultMap = new HashMap();
        one.setPhone("");
        one.setPassword("");
        one.setEmail("");
        resultMap.put("user",one);
        Map claimMaps = new HashMap();
        claimMaps.put("userId",one.getId());
        claimMaps.put("name",one.getName());
        String token = AppJwtUtil.getToken(claimMaps);
        resultMap.put("token",token);
        return ResponseResult.okResult(resultMap);
    }
}
